<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="description" content="Linux系统的远程登录"/>
    <meta name="keywords"
          content="nginx,freebsd,linux,mysql,shell,apache,centos,redhat,rhel,windows,sql,系统运维,IT运维,开源系统,开源系统运维"/>
    <title> Linux系统的远程登录 </title>

</head>
<body bgcolor="#FFFFFF" text="#000000">
<div id="menu">
    <ul>
		<span style="FONT-SIZE: 10.5pt">
			<li><a href="index.htm" hidefocus="true" id="mn_list">linux入门教程</a></li>

		</span>
    </ul>
</div>
<p style="text-align: center;"><span style="font-family:'宋体';font-size:15pt;"><b>第五章</b></span><span
        style="font-family:'Calibri';font-size:10.5pt;"> </span><span style="font-family:'Calibri';font-size:15pt;"><b>Linux</b></span><span
        style="font-family:'宋体';font-size:15pt;"><b>系统的远程登录</b></span></p>
<p style="text-align: justify;text-indent: 21pt;"><span style="font-family:'宋体';font-size:10.5pt;">首先要说一下，该部分内容对于</span><span
        style="font-family:'Calibri';font-size:10.5pt;">linux</span><span style="font-family:'宋体';font-size:10.5pt;">初学者来讲并不是特别重要的，可以先跳过该章节，先学下一章，等学完后再回来看这一章</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p style="text-align: justify;text-indent: 21pt;"><span
        style="font-family:'Calibri';font-size:10.5pt;">Linux</span><span style="font-family:'宋体';font-size:10.5pt;">大多应用于服务器，而服务器不可能像</span><span
        style="font-family:'Calibri';font-size:10.5pt;">PC</span><span style="font-family:'宋体';font-size:10.5pt;">一样放在办公室，它们是放在</span><span
        style="font-family:'Calibri';font-size:10.5pt;">IDC</span><span style="font-family:'宋体';font-size:10.5pt;">机房的，所以我平时登录</span><span
        style="font-family:'Calibri';font-size:10.5pt;">linux</span><span style="font-family:'宋体';font-size:10.5pt;">系统都是通过远程登录的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。Linux</span><span style="font-family:'宋体';font-size:10.5pt;">系统中是通过</span><span
        style="font-family:'Calibri';font-size:10.5pt;">ssh</span><span style="font-family:'宋体';font-size:10.5pt;">服务实现的远程登录功能</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">默认</span><span
        style="font-family:'Calibri';font-size:10.5pt;">ssh</span><span
        style="font-family:'宋体';font-size:10.5pt;">服务开启了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">22</span><span style="font-family:'宋体';font-size:10.5pt;">端口，而且当我们安装完系统时，这个服务已经安装，并且是开机启动的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">所以不需要我们额外配置什么就能直接远程登录</span><span
        style="font-family:'Calibri';font-size:10.5pt;">linux</span><span
        style="font-family:'宋体';font-size:10.5pt;">系统</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。ssh</span><span style="font-family:'宋体';font-size:10.5pt;">服务的配置文件为</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> /etc/ssh/sshd_config</span><span
        style="font-family:'宋体';font-size:10.5pt;">，你可以修改这个配置文件来实现你想要的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">ssh</span><span
        style="font-family:'宋体';font-size:10.5pt;">服务</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">比如你可以更改启动端口为</span><span
        style="font-family:'Calibri';font-size:10.5pt;">36000.</span></p>
<p style="text-align: justify;text-indent: 21pt;"><span style="font-family:'宋体';font-size:10.5pt;">如果你是</span><span
        style="font-family:'Calibri';font-size:10.5pt;">windows</span><span style="font-family:'宋体';font-size:10.5pt;">的操作系统，则</span><span
        style="font-family:'Calibri';font-size:10.5pt;">Linux</span><span style="font-family:'宋体';font-size:10.5pt;">远程登录需要在我们的机器上额外安装一个终端软件</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">目前比较常见的终端登录软件有</span><span
        style="font-family:'Calibri';font-size:10.5pt;">SecureCRT, Putty, SSH Secure Shell</span><span
        style="font-family:'宋体';font-size:10.5pt;">等，很多朋友喜欢用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">SecureCRT</span><span
        style="font-family:'宋体';font-size:10.5pt;">因为它的功能是很强大的，而笔者喜欢用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">Putty</span><span style="font-family:'宋体';font-size:10.5pt;">，只是因为它的小巧以及非常漂亮的颜色显示</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">不管你使用哪一个客户端软件，最终的目的只有一个，就是远程登录到</span><span
        style="font-family:'Calibri';font-size:10.5pt;">linux</span><span style="font-family:'宋体';font-size:10.5pt;">服务器上</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">这些软件网上有很多免费版的，你可以下载一个试着玩玩</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">下面笔者介绍如何使用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">Putty</span><span style="font-family:'宋体';font-size:10.5pt;">登录远程</span><span
        style="font-family:'Calibri';font-size:10.5pt;">linux</span><span
        style="font-family:'宋体';font-size:10.5pt;">服务器</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p style="text-align: justify;text-indent: 21pt;"><span style="font-family:'宋体';font-size:10.5pt;">如果你下载了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">putty</span><span style="font-family:'宋体';font-size:10.5pt;">，请双击</span><span
        style="font-family:'Calibri';font-size:10.5pt;">putty.exe </span><span
        style="font-family:'宋体';font-size:10.5pt;">然后弹出如下的窗口</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">笔者所用</span><span style="font-family:'Calibri';font-size:10.5pt;">putty</span><span
        style="font-family:'宋体';font-size:10.5pt;">为英文版的，如果你觉得英文的用着别扭，可以下载一个中文版的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p style="text-align: justify;"><span style="font-family:'Calibri';font-size:10.5pt;"><img src="5_1.png"></span></p>
<p style="text-align: justify;text-indent: 21pt;"><span style="font-family:'宋体';font-size:10.5pt;">因为是远程登录，所以你要登录的服务器一定会有一个</span><span
        style="font-family:'Calibri';font-size:10.5pt;">IP</span><span
        style="font-family:'宋体';font-size:10.5pt;">或者主机名</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">请在</span><span style="font-family:'Calibri';font-size:10.5pt;">Host Name( or IP address) </span><span
        style="font-family:'宋体';font-size:10.5pt;">下面的框中输入你要登录的远程服务器</span><span
        style="font-family:'Calibri';font-size:10.5pt;">IP(</span><span
        style="font-family:'宋体';font-size:10.5pt;">如果你的</span><span style="font-family:'Calibri';font-size:10.5pt;">linux</span><span
        style="font-family:'宋体';font-size:10.5pt;">还没有</span><span
        style="font-family:'Calibri';font-size:10.5pt;">IP</span><span style="font-family:'宋体';font-size:10.5pt;">，那么请自行设置一个</span><span
        style="font-family:'Calibri';font-size:10.5pt;">IP</span><span style="font-family:'宋体';font-size:10.5pt;">，如何设置请到后续章节查找</span><span
        style="font-family:'Calibri';font-size:10.5pt;">)</span><span
        style="font-family:'宋体';font-size:10.5pt;">，然后回车</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p style="text-align: justify;"><span style="font-family:'Calibri';font-size:10.5pt;"><img src="5_12.png"></span></p>
<p style="text-align: justify;"><span style="font-family:'宋体';font-size:10.5pt;">此时，提示我们输入要登录的用户名</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p style="text-align: justify;"><span style="font-family:'Calibri';font-size:10.5pt;"><img src="5_13.png"></span></p>
<p style="text-align: justify;"><span style="font-family:'宋体';font-size:10.5pt;">输入</span><span
        style="font-family:'Calibri';font-size:10.5pt;">root </span><span style="font-family:'宋体';font-size:10.5pt;">然后回车，再输入密码，就能登录到远程的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">linux</span><span
        style="font-family:'宋体';font-size:10.5pt;">系统了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p style="text-align: justify;"><span style="font-family:'Calibri';font-size:10.5pt;"><img src="5_14.png"></span></p>
<p style="text-align: justify;"><span style="font-family:'Calibri';font-size:10.5pt;">【</span><span
        style="font-family:'宋体';font-size:10.5pt;">使用密钥认证机制远程登录</span><span
        style="font-family:'Calibri';font-size:10.5pt;">linux】</span></p>
<p style="text-align: justify;text-indent: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">SSH</span><span
        style="font-family:'宋体';font-size:10.5pt;">服务支持一种安全认证机制，即密钥认证</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">所谓的密钥认证，实际上是使用一对加密字符串，一个称为公钥</span><span
        style="font-family:'Calibri';font-size:10.5pt;">(public key)</span><span
        style="font-family:'宋体';font-size:10.5pt;">，</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> </span><span style="font-family:'宋体';font-size:10.5pt;">任何人都可以看到其内容，用于加密；另一个称为密钥</span><span
        style="font-family:'Calibri';font-size:10.5pt;">(private key)</span><span
        style="font-family:'宋体';font-size:10.5pt;">，只有拥有者才能看到，用于解密</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。 </span><span style="font-family:'宋体';font-size:10.5pt;">通过公钥加密过的密文使用密钥可以轻松解密，但根据公钥来猜测密钥却十分困难</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。 ssh </span><span style="font-family:'宋体';font-size:10.5pt;">的密钥认证就是使用了这一特性</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">服务器和客户端都各自拥有自己的公钥和密钥</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。 </span><span style="font-family:'宋体';font-size:10.5pt;">如何使用密钥认证登录</span><span
        style="font-family:'Calibri';font-size:10.5pt;">linux</span><span style="font-family:'宋体';font-size:10.5pt;">服务器呢？</span>
</p>
<p style="text-align: justify;"><span style="font-family:'宋体';font-size:10.5pt;">首先使用工具</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> PUTTYGEN.EXE </span><span
        style="font-family:'宋体';font-size:10.5pt;">生成密钥对</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">打开工具</span><span style="font-family:'Calibri';font-size:10.5pt;">PUTTYGEN.EXE</span><span
        style="font-family:'宋体';font-size:10.5pt;">后如下图所示：</span></p>
<p style="text-align: justify;"><span style="font-family:'Calibri';font-size:10.5pt;"><img src="5_15.png"></span></p>
<p style="text-align: justify;"><span style="font-family:'宋体';font-size:10.5pt;">该工具可以生成三种格式的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">key </span><span
        style="font-family:'宋体';font-size:10.5pt;">：</span><span style="font-family:'Calibri';font-size:10.5pt;">SSH-1(RSA) SSH-2(RSA) SSH-2(DSA) </span><span
        style="font-family:'宋体';font-size:10.5pt;">，我们采用默认的格式即</span><span
        style="font-family:'Calibri';font-size:10.5pt;">SSH-2(RSA)。Number of bits in a generated key </span><span
        style="font-family:'宋体';font-size:10.5pt;">这个是指生成的</span><span style="font-family:'Calibri';font-size:10.5pt;">key</span><span
        style="font-family:'宋体';font-size:10.5pt;">的大小，这个数值越大，生成的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">key</span><span style="font-family:'宋体';font-size:10.5pt;">就越复杂，安全性就越高</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">这里我们写</span><span style="font-family:'Calibri';font-size:10.5pt;">2048.</span>
</p>
<p style="text-align: justify;"><span style="font-family:'Calibri';font-size:10.5pt;"><img
        src="5_16.png">开始生成密钥对：</span></p>
<p style="margin-right: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;"><img src="5_17.png">。</span></p>
<p style="text-align: justify;margin-right: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;"><img
        src="5_18.png"></span></p>
<p style="text-align: justify;margin-right: 21pt;"><span
        style="font-family:'宋体';font-size:10.5pt;">到这里，密钥对已经生成了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">你可以给你的密钥输入一个密码，（在</span><span
        style="font-family:'Calibri';font-size:10.5pt;">Key Passphrase</span><span
        style="font-family:'宋体';font-size:10.5pt;">那里）也可以留空</span><span style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">然后点</span><span style="font-family:'Calibri';font-size:10.5pt;"> Save public key </span><span
        style="font-family:'宋体';font-size:10.5pt;">保存公钥，点</span><span style="font-family:'Calibri';font-size:10.5pt;"> Save private Key </span><span
        style="font-family:'宋体';font-size:10.5pt;">保存私钥</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">笔者建议你放到一个比较安全的地方，一来防止别人偷窥，二来防止误删除</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">接下来就该到远程</span><span
        style="font-family:'Calibri';font-size:10.5pt;">linux</span><span style="font-family:'宋体';font-size:10.5pt;">主机上设置了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p style="text-align: justify;margin-right: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">1</span><span
        style="font-family:'宋体';font-size:10.5pt;">）创建目录</span><span style="font-family:'Calibri';font-size:10.5pt;"> /root/.ssh </span><span
        style="font-family:'宋体';font-size:10.5pt;">并设置权限</span></p>
<p style="text-align: justify;margin-right: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">[root@localhost ~]# mkdir  /root/.ssh  mkdir </span><span
        style="font-family:'宋体';font-size:10.5pt;">命令用来创建目录，以后会详细介绍，暂时只了解即可</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p style="text-align: justify;margin-right: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">[root@localhost ~]# chmod 700 /root/.ssh  chmod </span><span
        style="font-family:'宋体';font-size:10.5pt;">命令是用来修改文件属性权限的，以后会详细介绍</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p style="text-align: justify;margin-right: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">2</span><span
        style="font-family:'宋体';font-size:10.5pt;">）创建文件</span><span style="font-family:'Calibri';font-size:10.5pt;"> / root/.ssh/authorized_keys</span>
</p>
<p style="text-align: justify;margin-right: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">[root@localhost ~]# vim /root/.ssh/authorized_keys  vim </span><span
        style="font-family:'宋体';font-size:10.5pt;">命令是编辑一个文本文件的命令，同样在后续章节详细介绍</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p style="text-align: justify;margin-right: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">3</span><span
        style="font-family:'宋体';font-size:10.5pt;">）打开刚才生成的</span><span style="font-family:'Calibri';font-size:10.5pt;">public key </span><span
        style="font-family:'宋体';font-size:10.5pt;">文件，建议使用写字板打开，这样看着舒服一些，复制从</span><span
        style="font-family:'Calibri';font-size:10.5pt;">AAAA</span><span
        style="font-family:'宋体';font-size:10.5pt;">开头至</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> “</span><span style="font-family:'宋体';font-size:10pt;">---- END SSH2 PUBLIC KEY ----</span><span
        style="font-family:'Calibri';font-size:10pt;">“</span><span style="font-family:'宋体';font-size:10pt;"> 该行上的所有内容，粘贴到</span><span
        style="font-family:'Calibri';font-size:10.5pt;">/root/.ssh/authorized_keys </span><span
        style="font-family:'宋体';font-size:10.5pt;">文件中，要保证所有字符在一行</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">（可以先把复制的内容拷贝至记事本，然后编辑成一行载粘贴到该文件中）</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">在这里要简单介绍一下，如何粘贴，用</span><span
        style="font-family:'Calibri';font-size:10.5pt;">vim</span><span style="font-family:'宋体';font-size:10.5pt;">打开那个文件后，该文件不存在，所以</span><span
        style="font-family:'Calibri';font-size:10.5pt;">vim</span><span
        style="font-family:'宋体';font-size:10.5pt;">会自动创建</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">按一下字母</span><span
        style="font-family:'Calibri';font-size:10.5pt;">”i”</span><span
        style="font-family:'宋体';font-size:10.5pt;">然后同时按</span><span style="font-family:'Calibri';font-size:10.5pt;">shift + Insert </span><span
        style="font-family:'宋体';font-size:10.5pt;">进行粘贴（或者单击鼠标邮件即可），前提是已经复制到剪切板中了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">粘贴好后，然后把光标移动到该行最前面输入</span><span
        style="font-family:'Calibri';font-size:10.5pt;">ssh-ras </span><span style="font-family:'宋体';font-size:10.5pt;">，然后按空格</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">再按</span><span
        style="font-family:'Calibri';font-size:10.5pt;">ESC</span><span style="font-family:'宋体';font-size:10.5pt;">，然后输入冒号</span><span
        style="font-family:'Calibri';font-size:10.5pt;">wq </span><span
        style="font-family:'宋体';font-size:10.5pt;">即</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> :wq </span><span style="font-family:'宋体';font-size:10.5pt;">就保存了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span
        style="font-family:'宋体';font-size:10.5pt;">格式如下图：</span></p>
<p style="text-align: justify;margin-right: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;"><img
        src="5_19.png"></span></p>
<p style="text-align: justify;margin-right: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;">4</span><span
        style="font-family:'宋体';font-size:10.5pt;">）再设置</span><span style="font-family:'Calibri';font-size:10.5pt;">putty</span><span
        style="font-family:'宋体';font-size:10.5pt;">选项，点窗口左侧的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">SSh –&gt; Auth </span><span
        style="font-family:'宋体';font-size:10.5pt;">，单击窗口右侧的</span><span style="font-family:'Calibri';font-size:10.5pt;">Browse… </span><span
        style="font-family:'宋体';font-size:10.5pt;">选择刚刚生成的私钥，</span><span
        style="font-family:'Calibri';font-size:10.5pt;"> </span><span
        style="font-family:'宋体';font-size:10.5pt;">再点</span><span
        style="font-family:'Calibri';font-size:10.5pt;">Open </span><span style="font-family:'宋体';font-size:10.5pt;">，此时输入</span><span
        style="font-family:'Calibri';font-size:10.5pt;">root</span><span style="font-family:'宋体';font-size:10.5pt;">，就不用输入密码就能登录了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span></p>
<p style="text-align: justify;margin-right: 21pt;"><span style="font-family:'Calibri';font-size:10.5pt;"><img
        src="5_20.png"></span></p>
<p style="text-align: justify;margin-right: 21pt;"><span
        style="font-family:'宋体';font-size:10.5pt;">如果在前面你设置了</span><span
        style="font-family:'Calibri';font-size:10.5pt;">Key Passphrase </span><span
        style="font-family:'宋体';font-size:10.5pt;">，那么此时就会提示你输入密码的</span><span
        style="font-family:'Calibri';font-size:10.5pt;">。</span><span style="font-family:'宋体';font-size:10.5pt;">为了更加安全建议大家要设置一个</span><span
        style="font-family:'Calibri';font-size:10.5pt;">Key Passphrase。</span></p>
<p style="text-align: justify;"><span style="font-family:'宋体';font-size:10.5pt;">上一页 <a
        href="4.htm">初步进入linux世界</a></span></p>
<p style="text-align: justify;"><span style="font-family:'宋体';font-size:10.5pt;">下一页 <a
        href="6.htm">Linux文件与目录管理</a></span></p>
<p style="text-align: justify;"><span style="font-family:'宋体';font-size:10.5pt;"><a
        href="index.htm">回到首页</a>&nbsp;</span></p>

</body>
</html>